package com.yaoweigroup.community.ui.bm;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.entity.StringEntity;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.yaoweigroup.community.R;
import com.yaoweigroup.community.adapter.ShopCommentAdapter;
import com.yaoweigroup.community.config.Urls;
import com.yaoweigroup.community.enity.ShopComment;
import com.yaoweigroup.community.util.JsonUtil;
import com.yaoweigroup.community.util.LogUtil;
import com.yaoweigroup.community.util.UtilHttp;

public class CommentListActivity extends Activity implements OnClickListener {

	private RelativeLayout left_rl;
	private ListView listView;
	private ProgressBar progress;
	private ImageView error_iv;
	private ShopCommentAdapter adapter;
	private List<ShopComment> list;
	private ObjectMapper mapper;
	private TextView edit_tv;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_comment_list);

		mapper = new ObjectMapper();
		left_rl = (RelativeLayout) findViewById(R.id.left_rl);
		left_rl.setOnClickListener(this);
		error_iv = (ImageView) findViewById(R.id.error_iv);
		progress = (ProgressBar) findViewById(R.id.progress);
		listView = (ListView) findViewById(R.id.listView);
		list = new ArrayList<ShopComment>();
		adapter = new ShopCommentAdapter(this, list);
		listView.setAdapter(adapter);
		edit_tv = (TextView) findViewById(R.id.edit_tv);
		edit_tv.setOnClickListener(this);
		id = getIntent().getIntExtra("id", 0);

	}

	@Override
	protected void onResume() {
		obtainData();
		super.onResume();
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.left_rl:
			finish();
			break;
		case R.id.edit_tv:
			Intent intent = new Intent(this, CommentEditActivity.class);
			intent.putExtra("id", id);
			startActivity(intent);
			break;
		}
	}

	private int pageSize = 10;
	private int pageNo = 1;
	private int id;
	private List<ShopComment> comments;

	private void obtainData() {
		error_iv.setVisibility(View.GONE);
		HttpUtils http = UtilHttp.getHttpUtils(this);
		RequestParams params = new RequestParams();
		JsonUtil jsonUtil = new JsonUtil();
		jsonUtil.add("id", id);
		jsonUtil.add("pageNo", pageNo);
		jsonUtil.add("pageSize", pageSize);
		LogUtil.d("wxf", jsonUtil.toString());
		StringEntity enity = null;
		try {
			enity = new StringEntity(jsonUtil.toString());
		} catch (Exception e) {
			LogUtil.e("wxf", e.toString());
		}
		params.setContentType("application/json");
		params.setBodyEntity(enity);
		params.setHeader("Accept", "application/json");
		http.send(HttpRequest.HttpMethod.POST, Urls.getCommodityComment,
				params, new RequestCallBack<String>() {

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						LogUtil.d("wxf", "result = " + responseInfo.result);
						String result = responseInfo.result;
						try {
							JSONObject obj = new JSONObject(result);
							int state = obj.getInt("state");
							if (state == 200) {
								comments = mapper.readValue(
										obj.getString("data"),
										new TypeReference<List<ShopComment>>() {
										});
								handler.sendEmptyMessage(DATA_SUCCESS);
							} else {
								handler.sendEmptyMessage(DATA_FAIL);

							}
						} catch (Exception e) {
							handler.sendEmptyMessage(DATA_FAIL);

							LogUtil.e("wxf", e.toString());
						}

					}

					@Override
					public void onFailure(HttpException error, String msg) {
						LogUtil.e("wxf", error.toString());
						handler.sendEmptyMessage(DATA_FAIL);

					}

				});
	}

	public final int DATA_SUCCESS = 0x2210;
	public final int DATA_FAIL = DATA_SUCCESS + 1;
	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			progress.setVisibility(View.GONE);
			switch (msg.what) {
			case DATA_SUCCESS:
				if (comments != null && comments.size() > 0) {
					list.addAll(comments);
					adapter.setDataChange(comments);
				}
				if (list.size() == 0) {
					error_iv.setVisibility(View.VISIBLE);
					Toast.makeText(CommentListActivity.this, "暂无数据", 500)
							.show();
				}
				break;
			case DATA_FAIL:
				error_iv.setVisibility(View.VISIBLE);
				break;
			}
		}
	};

}
